﻿using System.IO;
using System.Windows.Threading;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Media;

namespace MushroomPicker
{
    public static class Sound
    {
        //static Stream stream_backsound { get { return TitleContainer.OpenStream("Sound/background.wma"); } }
        //static SoundEffect effect_backsound = SoundEffect.FromStream(stream_backsound);
        static SoundEffect effect_Exit = SoundEffect.FromStream(TitleContainer.OpenStream("Sound/exit.wav"));
        static SoundEffect effect_Wrong = SoundEffect.FromStream(TitleContainer.OpenStream("Sound/wrong.wav"));
        static SoundEffect effect_Win = SoundEffect.FromStream(TitleContainer.OpenStream("Sound/win.wav"));
        static SoundEffect effect_Click = SoundEffect.FromStream(TitleContainer.OpenStream("Sound/click.wav"));
        static bool _IsSound = true;
        static Song song_Background;
        public static void Initialization()
        {
            
        }
        public static void PlayBackSound()
        {
            if (IsMusicPlay == false || MediaPlayer.State == MediaState.Playing)
                return;
            FrameworkDispatcher.Update();
            if (song_Background == null)
            {
                song_Background = Song.FromUri("background", new System.Uri("Sound/background.mp3", System.UriKind.Relative));


                MediaPlayer.Volume = 0.5f;
                MediaPlayer.IsRepeating = true;
                MediaPlayer.Play(song_Background);
            }
            else
                MediaPlayer.Resume();
        }
        public static void StopBackSound()
        {
            if (song_Background != null)
            {
                FrameworkDispatcher.Update();
                MediaPlayer.Pause();
            }
        }
        public static bool IsSound
        {
            get { return IsMusicPlay && IsEffectSound; }
            set
            {
                _IsSound = value;
                IsMusicPlay = IsEffectSound = value;
            }
        }
        static bool _isEffectSound = true;

        public static bool IsEffectSound
        {
            get { return Sound._isEffectSound; }
            set
            {
                Sound._isEffectSound = value;
            }
        }

        static bool _isMusicPlay = true;
        public static bool IsMusicPlay
        {
            get { return _isMusicPlay; }
            set
            {
                _isMusicPlay = value;
                if (!_isMusicPlay)
                    StopBackSound();
                else
                    PlayBackSound();
            }
        }

        static float _Volume = 0.8f;
        public static float Volume
        {
            get { return _Volume; }
            set
            {
                _Volume = value;
                if (_Volume <= 0)
                    _Volume = 0;
                else
                    if (_Volume >= 1.0f)
                        _Volume = 1.0f;
            }
        }
        /*
        public static void PlayBackSound()
        {
            if (MediaPlayer.State == MediaState.Playing)
                return;
            if (!IsMusicPlay)
                return;
            if (loopPlay != null)
                return;
            if (!IsSound)
                return;
            if (effect_backsound == null)
                effect_backsound = SoundEffect.FromStream(stream_backsound);
            loopPlay = new DispatcherTimer();
            loopPlay.Interval = effect_backsound.Duration;
            loopPlay.Tick += (s, e) => { effect_backsound.Play(Volume, 0, 0); };
            FrameworkDispatcher.Update();
            effect_backsound.Play();
            loopPlay.Start();
        }
        public static void StopBackSound()
        {
            if (MediaPlayer.State == MediaState.Playing)
                return;
            if (loopPlay != null)
            {
                loopPlay.Stop();
                loopPlay = null;
            }
            if (effect_backsound != null)
            {
                effect_backsound.Dispose();
                effect_backsound = null;
            }
        }*/
        public static void PlayExit()
        {
            
            if (!_isEffectSound)
                return;
            FrameworkDispatcher.Update();
            effect_Exit.Play(Volume, 0, 0);
        }
        public static void PlayWrong()
        {
            
            if (!_isEffectSound)
                return;
            FrameworkDispatcher.Update();
            effect_Wrong.Play(Volume, 0, 0);
        }
        public static void PlayWin()
        {
            
            if (!_isEffectSound)
                return;
            FrameworkDispatcher.Update();
            effect_Win.Play(Volume, 0, 0);
        }

        public static void PlayClick()
        {
            
            if (!_isEffectSound)
                return;
            FrameworkDispatcher.Update();
            effect_Click.Play(Volume, 0, 0);
        }
    }
}
